import { createContext, useCallback, useEffect, useState } from 'react';
import { apiGetUserInfo } from '@/pages/_server/_api/Mine/apiGetUserInfo';
import { UserInfo_Web } from '@/pages/_server/_class/Mine/UserInfo';

export type ChagePersonalInfoContextType = {
    //当前显示得组件名
    showComponent: string;
    //设置显示哪个组件
    setShowComponent: (component: 'ChangeNickName' | 'BaseInfo' | 'ChangeAvatar') => any;
    //获取用户信息
    getUserInfo: () => any;
    /**用户信息 */
    userInfo: Partial<UserInfo_Web>;
};
export const ChagePersonalInfoContext = createContext<ChagePersonalInfoContextType>(undefined);
const useChagePersonalInfo = () => {
    //控制显示哪个组件
    const [showComponent, setShowComponent] = useState('BaseInfo');
    //用户信息
    const [userInfo, setUserInfo] = useState<Partial<UserInfo_Web>>({
        nickName: '',
        avatarUrl: '',
        contractAddress: '',
        userPhoneNumber: '',
    });
    const getUserInfo = useCallback(async () => {
        let res = await apiGetUserInfo();
        if (!res) return;
        setUserInfo(res.data);
    }, []);
    useEffect(() => {
        getUserInfo();
    }, []);
    return {
        getUserInfo,
        userInfo,
        showComponent,
        setShowComponent,
    };
};

export { useChagePersonalInfo };
